package net.sudot.chess.business.model;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.math.BigDecimal;
import java.time.Instant;

/**
 * 支付事务
 *
 * @author tangjialin on 2018-03-19.
 */
@Getter
@Setter
@Accessors(chain = true)
@Entity
public class PaymentTransaction extends BaseEntity<PaymentTransaction> {

    /** 类型 */
    public enum Type {
        /** target的值为OrderRecharge.id */
        RECHARGE("充值"),
//        GAME("游戏"),
//        BAIL("押金"),
        ;
        private String memo;

        Type(String memo) {
            this.memo = memo;
        }

        public String memo() {
            return memo;
        }
    }

    /** 编号 */
    private String sn;
    /** 类型 */
    @Enumerated(EnumType.STRING)
    private PaymentTransaction.Type type;
    /** 支付金额 */
    private BigDecimal amount;
    /** 是否成功 */
    private Boolean success;
    /** 支付插件ID */
    private String paymentPluginId;
    /** 支付插件名称 */
    private String paymentPluginName;
    /** 过期时间 */
    private Instant expire;
    /** 用户 */
    private Long userId;
    /** 支付对象 */
    private Long target;
    /** 外部支付系统编号 */
    private String extSn;

    /**
     * 判断是否已过期
     *
     * @return 是否已过期
     */
    @Transient
    public boolean hasExpired() {
        Instant expire = getExpire();
        return expire != null && expire.isBefore(Instant.now());
    }

}